{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4ba2627c-8a4f-40c1-a0e4-37e0398080de",
   "metadata": {},
   "source": [
    "1章作业：1 2 5 6 8题（包景东老师的书"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29da43ef-609a-45ca-8ab2-da3ea92d573f",
   "metadata": {},
   "source": [
    "# 1-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dced96e5-0007-4124-8e02-5a8d7d4771bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 已经手算过了，虽然结果不太对，但还是先过了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "967f0d2c-50e7-4c68-b0b4-8de86cc839ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "p,V,R,T,alpha,kappa = var(\"p,V,R,T,α,κ\")\n",
    "B=function(\"B\")(T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "88150e55-eaae-4a70-816e-6277973b838c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle V p = {\\left(p B\\left(T\\right) + 1\\right)} R T\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle V p = {\\left(p B\\left(T\\right) + 1\\right)} R T$"
      ],
      "text/plain": [
       "V*p == (p*B(T) + 1)*R*T"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "state_eq = p*V==R*T*(1+B*p) #物态方程\n",
    "show(state_eq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "d5e2d5a2-ab45-4a53-bf55-3b5978107bc8",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\frac{{\\left(T \\frac{\\partial}{\\partial T}B\\left(T\\right) + B\\left(T\\right)\\right)} p + 1}{T p B\\left(T\\right) + T}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\frac{{\\left(T \\frac{\\partial}{\\partial T}B\\left(T\\right) + B\\left(T\\right)\\right)} p + 1}{T p B\\left(T\\right) + T}$"
      ],
      "text/plain": [
       "((T*diff(B(T), T) + B(T))*p + 1)/(T*p*B(T) + T)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "V2 = V.subs(solve(state_eq, V)[0])\n",
    "alpha_expr = (1/V2) * diff(V2, T)\n",
    "show(alpha_expr.simplify_rational())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "97f572b0-0426-4b75-9357-9c0d30decc41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\frac{1}{p^{2} B\\left(T\\right) + p}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\frac{1}{p^{2} B\\left(T\\right) + p}$"
      ],
      "text/plain": [
       "1/(p^2*B(T) + p)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "kappa_expr = -(1/V2) * diff(V2, p)\n",
    "show(kappa_expr.simplify_full())"
   ]
  },
  {
   "cell_type": "raw",
   "id": "db581846-b1a7-4d7c-b52b-9337ab1e2ee2",
   "metadata": {},
   "source": [
    "?V.subs"
   ]
  },
  {
   "cell_type": "raw",
   "id": "d387427f-e820-4dec-98de-edcc0a7fab0f",
   "metadata": {},
   "source": [
    "?alpha_expr.simplify_rational"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f706367d-25bc-4bd6-b2f5-39c0f22769b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[V == (R*T*p*B(T) + R*T)/p]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solve(state_eq, V)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "19238db5-4fe0-4412-bc9d-2c2e5e615245",
   "metadata": {},
   "source": [
    "确实是偏导数，不过代换需要彻底一点"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "396bca0b-e5ed-41b9-a3aa-0e2bd7eacdb4",
   "metadata": {},
   "source": [
    "# 1-2"
   ]
  },
  {
   "cell_type": "raw",
   "id": "bab1bf90-f080-42e0-ac7a-e164b663a271",
   "metadata": {},
   "source": [
    "方程玩意儿，只是不知道这个A从哪里冒出来的\n",
    "其他的都好说\n",
    "目前暂时还整不出一个“类”……先单独语句，后续发现常用的东西积累多了再整一个类吧（类毕竟是高级语法）"
   ]
  },
  {
   "cell_type": "raw",
   "id": "2a64bb32-6c09-4485-9288-edf33b669ff1",
   "metadata": {},
   "source": [
    "按照书上的说法，这应该是要求解微分方程了"
   ]
  },
  {
   "cell_type": "raw",
   "id": "cf7164f7-d5ed-4664-b2f7-409dd5382821",
   "metadata": {},
   "source": [
    "alpha,kappa = var('α κ')\n",
    "show(alpha+kappa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1478d51c-7511-447e-b5da-6f21e7db7e29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\frac{\\frac{\\partial}{\\partial T}v\\left(T, p\\right)}{v\\left(T, p\\right)} = -\\frac{a - v\\left(T, p\\right)}{T v\\left(T, p\\right)}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\frac{\\frac{\\partial}{\\partial T}v\\left(T, p\\right)}{v\\left(T, p\\right)} = -\\frac{a - v\\left(T, p\\right)}{T v\\left(T, p\\right)}$"
      ],
      "text/plain": [
       "diff(v(T, p), T)/v(T, p) == -(a - v(T, p))/(T*v(T, p))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle -\\frac{\\frac{\\partial}{\\partial p}v\\left(T, p\\right)}{v\\left(T, p\\right)} = -\\frac{3 \\, {\\left(a - v\\left(T, p\\right)\\right)}}{4 \\, p v\\left(T, p\\right)}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle -\\frac{\\frac{\\partial}{\\partial p}v\\left(T, p\\right)}{v\\left(T, p\\right)} = -\\frac{3 \\, {\\left(a - v\\left(T, p\\right)\\right)}}{4 \\, p v\\left(T, p\\right)}$"
      ],
      "text/plain": [
       "-diff(v(T, p), p)/v(T, p) == -3/4*(a - v(T, p))/(p*v(T, p))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "T,p,a = var('T,p,a')\n",
    "\n",
    "v=function(\"v\")(T,p)\n",
    "#alpha,kappa = var(\"α,κ\")\n",
    "# alpha = function(\"α\")(V,T)= 1/V * (diff(V,T))  #但是V与T有个关系式……或许assume？但目前还不确定此式子\n",
    "# kappa = function(\"κ\")(V,p)= -1/V * (diff(V,p))\n",
    "\n",
    "# alpha(v,T)= 1/v * (diff(v,T))  #但是V与T有个关系式……或许assume？但目前还不确定此式子\n",
    "# kappa(v,p)= -1/v * (diff(v,p))\n",
    "#目前这个diff还是会直接变成0，而不是一个表达式的样子……真要把V变成T的函数么\n",
    "#alpha与kappa似乎不能直接这么引入，不然后面这些式子就变0了？\n",
    "\n",
    "eq1 = 1/v * diff(v,T) == (v-a)/(T*v)  # alpha的定义式代进去就是个偏微分方程了\n",
    "eq2 = -1/v * (diff(v,p)) ==3*(v-a)/(4*p*v)\n",
    "show(eq1)\n",
    "show(eq2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "666e38b4-88c3-4833-baad-9ac964c6be48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\frac{\\partial}{\\partial T}v\\left(T, p\\right)\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\frac{\\partial}{\\partial T}v\\left(T, p\\right)$"
      ],
      "text/plain": [
       "diff(v(T, p), T)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<html>\\(\\displaystyle \\frac{\\frac{\\partial}{\\partial T}v\\left(T, p\\right)}{v\\left(T, p\\right)} = -\\frac{a - v\\left(T, p\\right)}{T v\\left(T, p\\right)}\\)</html>"
      ],
      "text/latex": [
       "$\\displaystyle \\frac{\\frac{\\partial}{\\partial T}v\\left(T, p\\right)}{v\\left(T, p\\right)} = -\\frac{a - v\\left(T, p\\right)}{T v\\left(T, p\\right)}$"
      ],
      "text/plain": [
       "diff(v(T, p), T)/v(T, p) == -(a - v(T, p))/(T*v(T, p))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# T,p,a = var('T,p,a')\n",
    "v=function(\"v\")(T,p)\n",
    "show(diff(v,T))\n",
    "eq1 = 1/v * diff(v,T) == (v-a)/(T*v)\n",
    "show(eq1)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "a6df974f-7ebd-4b6b-a1c2-e06cacfb11ba",
   "metadata": {},
   "source": [
    "?function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1ee23e15-922f-4212-b2bf-a78eda6e405a",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'V' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m alpha \u001b[38;5;241m==\u001b[39m Integer(\u001b[38;5;241m1\u001b[39m)\u001b[38;5;241m/\u001b[39m\u001b[43mV\u001b[49m \u001b[38;5;241m*\u001b[39mV\u001b[38;5;241m.\u001b[39mpartial(T) \u001b[38;5;66;03m#方程关系……且这里保持p不变……怎么用sagemath表示这个含义？\u001b[39;00m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'V' is not defined"
     ]
    }
   ],
   "source": [
    "alpha == 1/V *V.partial(T) #方程关系……且这里保持p不变……怎么用sagemath表示这个含义？"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 10.5",
   "language": "sage",
   "name": "sagemath"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
